package com.yuju.group.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yuju.common.core.dao.IBaseDao;
import com.yuju.common.core.service.AbstractBaseService;
import com.yuju.group.dao.IMemberLevelDao;
import com.yuju.group.dao.entity.MemberLevel;
import com.yuju.group.model.MemberLevelModel;
import com.yuju.group.service.IMemberLevelService;
import com.yuju.util.Utils;
/**
 * 
 * 类名：MemberLevelServiceImpl  <br />
 *
 * 功能：
 *
 * @author : zhousc <br />
 * @Date : 2017年11月27日 上午10:34:03  <br />
 * @version : 1.1 <br />
 */
@Service
public class MemberLevelServiceImpl extends AbstractBaseService<MemberLevelModel, MemberLevel, String> implements IMemberLevelService{

	@Autowired
	private IMemberLevelDao memberLevelDao;
	
	@Override
	public IBaseDao<MemberLevelModel, MemberLevel, String> getDao() {
		// TODO Auto-generated method stub
		return memberLevelDao;
	}

	/**
	 * 
	 * 功能: <br/>会员级别新增
	 * 		
	 * 重写：zhousc <br/>
	 * 
	 * @Date ：2017年11月22日 下午4:30:15<br/>
	 * 
	 * @param memberLevelModel
	 * @return <br/>
	 * @see com.yuju.common.core.service.AbstractBaseService#save(java.lang.Object)
	 */
	@Override
	public int save(MemberLevelModel memberLevelModel){
		memberLevelModel.setId(Utils.getUUID());
		memberLevelModel.setGmtCreate(new Date());
		memberLevelModel.setGmtModified(new Date());
		return this.getDao().saveSelective(memberLevelModel);
	}
	
	/**
	 * 
	 * 功能: <br/>会员级别更新
	 * 
	 * 重写：zhousc <br/>
	 * 
	 * @Date ：2017年11月23日 上午9:43:32<br/>
	 * 
	 * @param memberLevelModel
	 * @return <br/>
	 * @see com.yuju.common.core.service.AbstractBaseService#updateSelective(java.lang.Object)
	 */
	@Override
	public int updateSelective(MemberLevelModel memberLevelModel){
		memberLevelModel.setGmtModified(new Date());
		return this.getDao().updateSelective(memberLevelModel);
	}

	/**
	 * 
	 * 功能:重名校验 <br/>
	 * 
	 * 重写：zhousc <br/>
	 * 
	 * @Date ：2017年12月13日 下午2:08:31<br/>
	 * 
	 * @param memberLevelModel
	 * @return <br/>
	 * @see com.yuju.group.service.IMemberLevelService#selectByName(com.yuju.group.model.MemberLevelModel)
	 */
	@Override
	public boolean selectByName(MemberLevelModel memberLevelModel) {
		//定义返回值
		boolean result = false;
		//装载数据查询数据库
		MemberLevelModel model = new MemberLevelModel();
		model.setName(memberLevelModel.getName());
		model.setGroupId(memberLevelModel.getGroupId());
		
		//修改时验证重名
		if (Utils.isNotNull(memberLevelModel.getId())) {
			MemberLevelModel memberLevelModel1 = convert(this.getDao().getObj(memberLevelModel.getId()));
			if (memberLevelModel.getName().equals(memberLevelModel1.getName())) {
		            result = true;
		        } else {
		       	 	Map<Object, Object> beanMap = getBeanMap(model);
		            List<MemberLevelModel> list = this.getDao().list(beanMap);
		            if (list.size() == 0) {
		                result = true;
		            }
		        }
		//新增验证重名
		}else {
			Map<Object, Object> beanMap = getBeanMap(model);
		        List<MemberLevelModel> list = this.getDao().list(beanMap);
		        if (list.size() == 0) {
		            result = true;
		        }
		    }
		return result;
	}
	
	
}
